home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / AppleScript for Acrobat plug-in / SOURCES / STAMPUI.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  10.3 KB  |  314 lines

  1. /******************************************************************************
  2.  
  3. StampUI.c
  4.  
  5. This file is furnished to you by Adobe Systems Incorporated 
  6. under the terms of the Acrobat(r) Plug-ins Software 
  7. Development Kit License Agreement.
  8.  
  9. Copyright (C) 1994-1997, Adobe Systems Inc.  All Rights Reserved.
  10.  
  11.  
  12. Implementation of the Stamper plug-in's Stamping tool
  13. and associated UI elements.
  14.  
  15. ******************************************************************************/
  16.  
  17. #include "stampui.h"
  18. #include "stamppr.h"
  19. #include "stamper.h"
  20. #include "stampah.h"
  21.  
  22. #include <string.h>
  23.  
  24. #include "AVCalls.h"
  25. #include "CorCalls.h"
  26. #include "PDCalls.h"
  27.  
  28. static AVCursor stamperCursor;
  29. static AVToolRec stamperTool;
  30. static AVToolButton stamperToolButton;
  31. static AVMenuItem stamperMenuItem;
  32. static AVMenuItem aboutMenuItem ;
  33. static void SetUpMenuItem(void);
  34.  
  35. static ACCB1 void ACCB2 StampAbout(void *data);
  36.  
  37.     /* Stamper AVTool callbacks */
  38. static ACCB1 ASAtom ACCB2 StamperToolGetType(AVTool tool)
  39. {
  40.     return Stamper_K;
  41. }
  42.  
  43. static ASBool gStamperIsPersistent;
  44. static ACCB1 void ACCB2 StamperActivate(AVTool tool, ASBool persistent)
  45. {
  46.     gStamperIsPersistent = persistent;
  47. }
  48.     
  49. static ACCB1 ASBool ACCB2 StamperAdjustCursor(AVTool tool, AVPageView pageView, Int16 x, Int16 y)
  50. {
  51.     PDAnnot hitAnnot;
  52.     ASBool shiftKeyIsDown = ((AVSysGetModifiers() & AV_SHIFT) != 0);
  53.         /* Defer to the annotation, if any */
  54.     if (!shiftKeyIsDown && AVPageViewIsAnnotAtPoint(pageView, x, y, &hitAnnot))
  55.         return false;
  56.             
  57.     AVSysSetCursor(stamperCursor);
  58.     return true;
  59. }
  60.     
  61.     static ACCB1 ASBool ACCB2 StamperDoClick(AVTool tool, AVPageView pageView,
  62.                     Int16 xHit, Int16 yHit,
  63.                     Int16 flags,
  64.                     Int16 clickNo)
  65.     {
  66.     
  67.         PDAnnot hitAnnot;
  68.         ASBool shiftKeyIsDown = ((AVSysGetModifiers() & AV_SHIFT) != 0);
  69.  
  70.         if (!shiftKeyIsDown && AVPageViewIsAnnotAtPoint(pageView, xHit, yHit, &hitAnnot))
  71.             return false;
  72.         
  73.         if (CreateAppleScriptAnnotationAt(pageView, xHit, yHit))
  74.         {
  75.             /* If we're not in a persistent mode, select the last active
  76.             ** tool now that the user's done with the Stamper.
  77.             */
  78.             if (!gStamperIsPersistent)
  79.                 AVAppSetActiveTool(AVAppGetLastActiveTool(), true);
  80.         }
  81.         
  82.         return true;
  83.     }
  84.  
  85.  
  86.     /* setStamperTool
  87.     ** This is the AVExecuteProc associated with the AVMenuItem and AVToolButton
  88.     ** created by the Stamper.
  89.     */
  90. static ACCBPROTO1 void (ACCBPROTO2 *setStamperToolCallback)(void *clientData);
  91. static ACCB1 void ACCB2 setStamperTool(void *clientData)
  92. {
  93.     /* As with all the other tools, the Stamper tool should be
  94.     ** sticky if the user holds down the option key while selecting it.
  95.     */
  96.     AVAppSetActiveTool(&stamperTool, (ASBool)((AVSysGetModifiers() & AV_OPTION) != 0));
  97. }
  98.     
  99. /* stamperIsEnabled
  100. ** This is the AVComputeEnabledProc associated with the AVMenuItem, AVToolButton
  101. ** and AVTool created by the Stamper.
  102. */
  103. static ACCBPROTO1 ASBool (ACCBPROTO2 *stamperIsEnabledCallback)(void *permRequired);
  104. static ACCB1 ASBool ACCB2 stamperIsEnabled(void *permRequired)
  105. {
  106.     AVDoc avDoc = AVAppGetActiveDoc();
  107.     if (!avDoc)
  108.         return false;
  109.     else {
  110.         PDPerms docPerms = PDDocGetPermissions(AVDocGetPDDoc(avDoc));
  111.         return (!permRequired || (((PDPerms)permRequired & docPerms) != 0));
  112.     }    
  113. }
  114.     
  115. /* stamperIsMarked
  116. ** This is the AVComputeMarkedProc associated with the AVMenuItem and AVToolButton
  117. ** created by the Stamper.
  118. */
  119. static ACCBPROTO1 ASBool (ACCBPROTO2 *stamperIsMarkedCallback)(void *clientData);
  120. static ACCB1 ASBool ACCB2 stamperIsMarked(void *clientData)
  121. {
  122.     return ((AVAppGetActiveDoc() != NULL) &&
  123.     (AVAppGetActiveTool() == &stamperTool));
  124. }
  125.         
  126. /* SetUpTool
  127. ** Sets up the Stamper's AVToolRec.  Note that AVTools are distinct from,
  128. ** but often invoked by, AVToolButtons.
  129. */
  130. static void SetUpTool(void)
  131. {
  132.     /* Set up the AVToolRec.  Don't forget to set the size field! */
  133.     memset(&stamperTool, 0, sizeof(AVToolRec));
  134.     stamperTool.size = sizeof(AVToolRec);
  135.  
  136.     stamperTool.Activate = ASCallbackCreateProto(ActivateProcType, &StamperActivate);
  137.     stamperTool.GetType = ASCallbackCreateProto(GetTypeProcType, &StamperToolGetType);
  138.     stamperTool.AdjustCursor = ASCallbackCreateProto(AdjustCursorProcType, &StamperAdjustCursor);
  139.     stamperTool.DoClick = ASCallbackCreateProto(DoClickProcType, &StamperDoClick);
  140.     /* The stamperIsEnabledCallback is shared between the AVTool,
  141.     ** AVToolButton and AVMenuItem; no need to ASCallbackCreate() it
  142.     ** three times.
  143.     */
  144.     stamperTool.ComputeEnabled = stamperIsEnabledCallback;
  145.     stamperTool.computeEnabledData = NULL;
  146.     
  147.     AVAppRegisterTool(&stamperTool);
  148.     
  149.     AVAppUnregisterNotification(AVAppDidInitializeNSEL,0, SetUpTool, NULL);
  150. }
  151.  
  152. static void SetUpToolButton(void)
  153. {
  154.     /* Insert the Stamper tool button just before the "endToolsGroup"
  155.     ** AVToolButton separator.
  156.     */
  157.     void *stamperIcon = GetStamperToolButtonIcon();
  158.     AVToolBar bar = AVAppGetToolBar();
  159.     AVToolButton separator = AVToolBarGetButtonByName(bar, ASAtomFromString("endToolsGroup"));
  160.     
  161.     stamperToolButton = AVToolButtonNew(Stamper_K, stamperIcon, true, false);
  162.     AVToolButtonSetExecuteProc(stamperToolButton, setStamperToolCallback, NULL);
  163.     AVToolButtonSetComputeEnabledProc(stamperToolButton, stamperIsEnabledCallback, (void *)pdPermEdit);
  164.     AVToolButtonSetComputeMarkedProc(stamperToolButton, stamperIsMarkedCallback, NULL);
  165.     AVToolButtonSetHelpText(stamperToolButton, "Selects Applescript tool button");
  166.  
  167.     AVToolBarAddButton(bar, stamperToolButton, true, separator);
  168. }
  169.  
  170. /* Display our About box */
  171. static ACCB1 void ACCB2 StampAbout(void *data)
  172. {
  173.     AVAlertNote( "AppleScript" );
  174. }
  175.  
  176. static void SetUpMenuItem(void)
  177. {
  178.     /* Add the "Stamper" menu item immediately after the thread
  179.     ** menu item.
  180.     */
  181.     AVMenu commandMenu, SDKaboutMenu, helpMenu, prefsMenu, toolsMenu;
  182.     AVMenubar MenuBar = AVAppGetMenubar();
  183.     AVMenuItem generalAboutMenuItem, menuItem, aboutMenuItem1, otherItem;    
  184.     void *stamperIcon;
  185.     Int32 index;
  186.  
  187.     int isNewCommandMenu = 0; /* A flag to check if the command menu exists */
  188.  
  189.     if (!MenuBar) 
  190.         return;
  191.  
  192.     /* Set up our command menuitem. */
  193.     commandMenu = AVMenubarAcquireMenuByName(MenuBar, "ADBE:File");
  194.     if (!commandMenu){
  195.         isNewCommandMenu = 1;
  196.         commandMenu = AVMenuNew ("Acrobat SDK", "ADBE:Acrobat_SDK",gExtensionID);
  197.     }
  198.  
  199.     menuItem = AVMenuItemNew("AppleScript", "ADBE:AppleScriptTool", NULL, false, NO_SHORTCUT, 0, NULL, gExtensionID);
  200.     AVMenuItemSetExecuteProc(menuItem, 
  201.             ASCallbackCreateProto(AVExecuteProc, &StampAbout), NULL);
  202.  
  203.     AVMenuAddMenuItem(commandMenu, menuItem, APPEND_MENUITEM);
  204.  
  205.     if (isNewCommandMenu){
  206.         AVMenubarAddMenu(MenuBar,commandMenu ,APPEND_MENU);
  207.         isNewCommandMenu = 0;
  208.     }
  209.  
  210.     AVMenuRelease(commandMenu);
  211.  
  212.     /* Install About command in Help pull-down menu. */
  213.  
  214.     /* Set up our "About" menuitem.
  215.        Its text does not include "About" because the viewer provides that. */
  216.  
  217.     SDKaboutMenu = AVMenubarAcquireMenuByName(MenuBar, "ADBE:AboutSDK");
  218.     
  219.     if (!SDKaboutMenu){
  220.         helpMenu = AVMenubarAcquireMenuByName(MenuBar, "Help");
  221.         SDKaboutMenu = AVMenuNew("About Acrobat SDK", "ADBE:AboutSDK", gExtensionID);
  222.         aboutMenuItem1 = AVMenuItemNew("About Acrobat SDK", "ADBE:AboutSDK", SDKaboutMenu, false, NO_SHORTCUT, 0, NULL, gExtensionID);
  223.         AVMenuAddMenuItem(helpMenu, aboutMenuItem1, APPEND_MENUITEM);
  224.     
  225.         generalAboutMenuItem = AVMenuItemNew("General", "ADBE:AboutGeneral", NULL, false, NO_SHORTCUT, 0, NULL, gExtensionID);
  226.         AVMenuAddMenuItem(SDKaboutMenu, generalAboutMenuItem, APPEND_MENUITEM);
  227.     }
  228.     
  229.     aboutMenuItem1 = AVMenuItemNew("Stamper", "ADBE:AboutStamp", NULL, false, NO_SHORTCUT, 0, NULL, gExtensionID);
  230.     AVMenuItemSetExecuteProc(aboutMenuItem1, 
  231.         ASCallbackCreateProto(AVExecuteProc, &StampAbout), NULL);
  232.     AVMenuAddMenuItem(SDKaboutMenu, aboutMenuItem1, APPEND_MENUITEM);
  233.     AVMenuRelease(SDKaboutMenu);
  234.     
  235.     toolsMenu = AVMenubarAcquireMenuByName(MenuBar, "Tools");
  236.     
  237.     /* Find the Thread tool; the Stamper should go right after it.
  238.     */
  239.     otherItem = AVMenubarAcquireMenuItemByName(MenuBar, "Thread");
  240.     stamperIcon = GetStamperMenuItemIcon();
  241.     index = AVMenuGetMenuItemIndex(toolsMenu, otherItem) + 1;
  242.     
  243.     stamperMenuItem = AVMenuItemNew("AppleScript", "ADBE:AppleScript", NULL, true,
  244.         NO_SHORTCUT, 0, stamperIcon, gExtensionID);
  245.     AVMenuItemSetExecuteProc(stamperMenuItem, setStamperToolCallback, NULL);
  246.     AVMenuItemSetComputeEnabledProc(stamperMenuItem, stamperIsEnabledCallback, (void *)pdPermEdit);
  247.     AVMenuItemSetComputeMarkedProc(stamperMenuItem, stamperIsMarkedCallback, NULL);
  248.     
  249.     /* AVMenuItems are added by index, not relative to other AVMenuItems,
  250.     ** unfortunately.  Oh well.
  251.     */
  252.     AVMenuAddMenuItem(toolsMenu, stamperMenuItem, index);
  253.     
  254.     /* Release the AVMenuItems that we don't need any more
  255.     */
  256.     AVMenuItemRelease(otherItem);
  257.     AVMenuRelease(toolsMenu);
  258. }
  259.  
  260. static ACCB1 void ACCB2 showAboutBox(void *clientData)
  261. {
  262.     AVAlertNote("Stamper v. 1.0.1");
  263. }
  264.     
  265. static void SetUpAboutBox(void)
  266. {
  267.      AVMenubar bar = AVAppGetMenubar();
  268.      AVMenu aboutExtensionsMenu = AVMenubarAcquireMenuByName(bar, "AboutExtensions");
  269.      aboutMenuItem = AVMenuItemNew("About AppleScript...", "ADBE:AboutAppleScript", NULL,
  270.          false, NO_SHORTCUT, 0, NULL, gExtensionID);
  271.     
  272.     AVMenuItemSetExecuteProc(aboutMenuItem, ASCallbackCreateProto(AVExecuteProc,
  273.         &showAboutBox), NULL);
  274.     AVMenuAddMenuItem(aboutExtensionsMenu, aboutMenuItem, APPEND_MENUITEM);
  275. }
  276.  
  277. void SetUpUI(void)
  278. {
  279.     Stamper_K = ASAtomFromString("ADBE:AppleScript");
  280.     
  281.     /* Create the execute, computeEnabled, and computeMarked callbacks here
  282.     ** because they're shared between the AVTool, AVToolButton and AVMenuItem.
  283.     */
  284.     setStamperToolCallback = ASCallbackCreateProto(AVExecuteProc, &setStamperTool);
  285.     stamperIsEnabledCallback = ASCallbackCreateProto(AVComputeEnabledProc, &stamperIsEnabled);
  286.     stamperIsMarkedCallback = ASCallbackCreateProto(AVComputeMarkedProc, &stamperIsMarked);
  287.     
  288.     stamperCursor = GetStamperCursor();
  289.     
  290.     AVAppRegisterNotification(AVAppDidInitializeNSEL,0, SetUpTool, NULL);
  291.  
  292.     if(ASGetConfiguration(ASAtomFromString("CanEdit"))){ 
  293.         SetUpToolButton();
  294.         SetUpMenuItem();
  295.     }
  296. }
  297.  
  298. void CleanUpUI(void)
  299. {
  300.     
  301.     AVAppUnregisterNotification(AVAppDidInitializeNSEL,0, SetUpTool, NULL);
  302.     if(stamperToolButton){ 
  303.         AVToolButtonDestroy(stamperToolButton);
  304.         }
  305.     if(stamperMenuItem){ 
  306.         AVMenuItemRemove(stamperMenuItem);
  307.         AVMenuItemRelease(stamperMenuItem);
  308.         }
  309.         if(aboutMenuItem){ 
  310.         AVMenuItemRemove(aboutMenuItem);
  311.         AVMenuItemRelease(aboutMenuItem);
  312.         }
  313.  
  314. }